package com.example.friend.service.exam;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.constants.Constants;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.utils.ThreadLocalUtil;
import com.example.friend.domain.exam.dto.ExamQueryDTO;
import com.example.friend.domain.exam.vo.ExamVO;
import com.example.friend.domain.user.User;
import com.example.friend.manager.ExamCacheManager;
import com.example.friend.mapper.exam.ExamMapper;
import com.example.friend.mapper.user.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2025/3/8 20:03
 */
@Service
public class ExamServiceImpl implements ExamService{

    @Resource
    private ExamMapper examMapper;
    @Resource
    private ExamCacheManager examCacheManager;
    @Resource
    private UserMapper userMapper;

    //从数据库中获取竞赛列表
    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        PageHelper.startPage(examQueryDTO.getPageNum(),examQueryDTO.getPageSize());
        return examMapper.selectExamList(examQueryDTO);
    }

    //从redis中查询竞赛列表
    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        //判断redis中的 未完赛或历史竞赛 的list是否有数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        List<ExamVO> examVOList;
        if (total == null || total ==0){
            //走数据库查询
            examVOList = list(examQueryDTO);
            //如果redis中没有数据 刷新缓存
            examCacheManager.refreshCache(examQueryDTO.getType(),null);
            total = new PageInfo<>(examVOList).getTotal();
        }else{
            //从redis中获取数据
            examVOList = examCacheManager.getExamVOList(examQueryDTO,null);
            total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        }
        if (CollectionUtil.isEmpty(examVOList)){
            return TableDataInfo.empty();
        }
        //判断拿到的这些竞赛，用户是否报名了
        assembleExamVOList(examVOList);
        return TableDataInfo.success(examVOList,total);
    }

    //判断拿到的竞赛列表当前登录用户是否报名
    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID,Long.class);
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)){
            return;
        }
        for (ExamVO examVO:examVOList){
            if (userExamIdList.contains(examVO.getExamId())){
                examVO.setEnter(true);
            }
        }
    }

    //刷新redis的缓存 未完赛和历史竞赛的缓存
    @Override
    public void refreshCache() {
        //刷新未开赛列表
        examCacheManager.refreshCache(0,null);
        //刷新历史竞赛列表
        examCacheManager.refreshCache(1,null);
        //刷新所有用户的报名竞赛列表
        List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>());
        for (User user:users){
            examCacheManager.refreshCache(2,user.getUserId());
        }
        //TODO: 刷新题目的redis列表

    }

    //获取竞赛中的第一个题目
    @Override
    public String getFirstQuestion(Long examId) {
        //判断redis中是否有数据
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize==null || listSize<=0){
            examCacheManager.refreshExamQuestionCache(examId);
        }
        return String.valueOf(examCacheManager.getFirstQuestion(examId));
    }

    //获取竞赛中的上一题
    @Override
    public String preQuestion(Long examId, Long questionId) {
        //判断Redis中是否有数据
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize==null || listSize<=0){
            examCacheManager.refreshExamQuestionCache(examId);
        }
        //从缓存中拿数据
        return String.valueOf(examCacheManager.preQuestion(examId,questionId));
    }

    //获取竞赛的下一题
    @Override
    public String nextQuestion(Long examId, Long questionId) {
        //判断Redis中是否有数据
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize==null || listSize<=0){
            examCacheManager.refreshExamQuestionCache(examId);
        }
        //从缓存中拿数据
        return String.valueOf(examCacheManager.nextQuestion(examId,questionId));
    }


}
